﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Application.Core.EntityService;
using Application.Core.ErrorHandlers;
using Domain.MainModule.Entities;
using Domain.MainModule.Repositories;
using Infrastructure.CrossCutting.Caching;
using System.ServiceModel.Activation;

namespace Application.MainModule.UserManagement
{
    [ApplicationErrorHandler()]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class UserManagementService : BaseService<Customers, int>, IUserManagementService
    {
        private readonly ICustomerRepository _customerRepository;
        private readonly ICache _cache;
        public UserManagementService(ICustomerRepository customerRepository, ICache cache)
            : base(customerRepository, cache)
        {
            _customerRepository = customerRepository;
            _cache = cache;
        }
        public IEnumerable<Customers> GetUsers()
        {
            return GetAll();
        }

       
        public bool ValidateUser(string userName, string password)
        {
            var customers = _customerRepository.GetFiltered(p => p.CustomerName == userName && p.CustomerPassword == password && p.IsRegister == true);
            return customers.Any();
        }

        public Customers GetCanonicalUser(int customerId)
        {
            return _customerRepository.GetFiltered(p => p.CustomerId == customerId && p.IsActive == true).SingleOrDefault();
        }

        public Customers GetCanonicalUser(string authorizationId)
        {
            return _customerRepository.GetFiltered(p => p.AuthorizationId == authorizationId && p.IsActive == true).SingleOrDefault();

        }

        public Customers CreateUser(string userName, string password, string email)
        {
            try
            {
                var customer = new Customers()
                                   {
                                       CustomerName = userName,
                                       AuthorizationId = userName,
                                       CustomerPassword = password,
                                       CustomerEmail = email,
                                       IsActive = true,
                                       IsRegister = true
                                   };
                _customerRepository.Add(customer);
                _customerRepository.UnitOfWork.CommitAndRefreshChanges();
                return customer;
            }catch
            {
                return null;
            }
        }

        public Customers CreateUser(string userName, string authorizationId)
        {
            var customer = new Customers()
            {
                CustomerName = userName,
                AuthorizationId = authorizationId,
                IsActive = true,
                IsRegister = false
            };
            _customerRepository.Add(customer);
            _customerRepository.UnitOfWork.CommitAndRefreshChanges();
            return customer;
        }


        public bool ChangePassword(string userName, string oldPassword, string newPassword)
        {
            return true;
        }
    }
}
